package model

import (
	"fmt"
	"choujiang/common/db"
)

type UserExtra struct {
	Id           int64   `xorm:"INT(11) not null pk autoincr"`
	UserId       int64   `xorm:"not null default 0 INT(11) index"`
	ParentId     int64   `xorm:"not null default 0 INT(11) index"`
	Balance      float64 `xorm:"not null default 0 DECIMAL(10)"`
	Point        int64   `xorm:"not null default 0 INT(11)"`
	Receiver     string  `xorm:"VARCHAR(255)"`
	Phone        string  `xorm:"VARCHAR(255)"`
	Province     int64   `xorm:"not null default 0 INT(11)"`
	City         int64   `xorm:"not null default 0 INT(11)"`
	Area         int64   `xorm:"not null default 0 INT(11)"`
	Address      string  `xorm:"VARCHAR(255)"`
	GroupPoint   int64   `xorm:"INT(11) not null default 0"`
	QrPath       string  `xorm:"VARCHAR(255)"`
	AccumBalance float64 `xorm:"DECIMAL(10) not null default 0"`
	AccumPromote int64   `xorm:"not null default 0 INT(11)"`
	Coupon       int64   `xorm:"not null default 0 INT(11)"`
}

const (
	INIT_BALANCE = 0 //初始balance
	INIT_POINT   = 0 //初始point
)

func (u *UserExtra) Insert() (*UserExtra, error) {
	_, err := db.MysqlEngine.InsertOne(u)
	return u, err
}

func (u *UserExtra) Update() (*UserExtra, error) {
	_, err := db.MysqlEngine.ID(u.Id).Update(u)
	return u, err
}

func GetUserExtraByUid(uid int64) *UserExtra {
	userExtra := new(UserExtra)
	has, err := db.MysqlEngine.Where("user_id=?", uid).Get(userExtra)
	fmt.Printf("GetUserExtraByUid err %+v\n", err)
	if !has || err != nil {
		return nil
	}
	return userExtra
}
